 
Ext.namespace("Ext.un");

Ext.un.TriggerTemplet = function(config){
	config || (config = {});
	if(!config.store || !config.cm || !config.triggerForm){
		alert("请确保属性中包含store,cm,Formpanel配置信息");
	}
	if(config.store){
		this.store = config.store;
	}
	if(config.cm){
		this.cm = config.cm;
	}
	if(config.sm){
		this.sm = config.sm;
	}
	if(config.triggerForm){
		this.triggerForm = config.triggerForm;
	}
	if(config.onTriggerClick){
		this.onTriggerClick = config.onTriggerClick;
	}
	if(config.onRowClick){
		this.onRowClick = config.onRowClick;
	}
	if(config.onClear){
		this.onClear = config.onClear;
	}
	if(config.isMenu){
		this.isMenu = config.isMenu;
	}
	Ext.un.TriggerTemplet.superclass.constructor.apply(this,arguments);
}

Ext.extend(Ext.un.TriggerTemplet,Ext.form.TriggerField,{
	triggerClass: 'x-form-ref-trigger',
	store:new Ext.data.ArrayStore(),
	cm:new Ext.grid.ColumnModel([]),
	triggerForm:new Ext.form.FormPanel(),
	isMenu:false,
	showContainer:null,
	sm:new Ext.grid.CheckboxSelectionModel(),
	
	initComponent: function() {
        Ext.form.ComboBox.superclass.initComponent.call(this);   
        this.triggerGrid = new Ext.grid.GridPanel({
        		deferRowRender:false,
	            store: this.store, 
	            cm: this.cm,
	            sm: this.sm,
	            height:200,
	        	stripeRows:true,
	        	viewConfig:{forceFit:true},
	        	loadMask:true,
	        	frame:true,
	        	bbar:new Ext.PagingToolbar({
				 pageSize: 10,
			     displayInfo: true,
			     store: this.store,
			     displayMsg: '显示第{0}条到{1}条记录，一共{2}条',  
			     emptyMsg:'没有记录',
			     doLoad : function(start){
			        var o = {}, pn = this.getParams();
			        o[pn.start] = start;
			        o[pn.limit] = this.pageSize;
			        o["rp"] = this.pageSize;
			        o['page'] = start / this.pageSize + 1;
			        if(this.fireEvent('beforechange', this, o) !== false){
			            this.store.load({params:o});
			        }
			    }
			})
        });
       this.triggerPanel = new Ext.Panel({
       		autoHeight:true,
    		items:[this.triggerForm,this.triggerGrid]
    	});
    	
    	if(!this.isMenu){
    		this.showContainer = new Ext.Window({
	    		autoHeight:true,
	    		width:600,
	    		closeAction : "hide",
	    		items:[this.triggerPanel]
	    	});
    	}else{
    		this.showContainer = new Ext.menu.Menu({
	       		autoHeight:true,
	    		width:600,
	    		items:[this.triggerPanel]
	    	});	
    	}
    	if(this.isMultiple){
    		this.onCellClick(this);
    	}else{
    		this.onRowClick(this);
    	}
    },
    onCellClick:function(trigger){
    	 this.triggerGrid.on('cellclick',function(grid,rowIndex,e){
	    	alert(rowIndex);
	 	});
    },
    onRowClick:function(trigger){
	    this.triggerGrid.on('rowdblclick',function(grid,rowIndex,e){
	    	alert(rowIndex);
	 	});
    },
    onReset:function(){
    		this.triggerForm.getForm().reset();
    		this.store.baseParams="";
    		this.store.load();
    },
    onClear:function(){
    		alert("onClear be running");
    },
    onQuery:function(){
    	var triggerForm = this.triggerForm;
	    var triggerGrid = this.triggerGrid;
	    	if(triggerForm && triggerForm.getForm().isValid()){
				var valObj = triggerForm.getForm().getValues(false);
				triggerGrid.store.baseParams = valObj;
				triggerGrid.store.load({params:{start:0,limit:10}});
			}
	},
	onTriggerClick:function(){
		alert("onTrigger events running");
	},
	hideWin:function(){
		//if(!this.isMenu){  why lose menu hide ?
    		this.showContainer.hide();
		//}
	}
});
